$(function() {
    var cookieKey = 'm_userdata';
    var data = {

    }

    function updateCookie() {
        var cookieData = JSON.stringify(data);
        $.cookie(cookieKey,cookieData);
        console.log(getData());
        calc();
    }

    function getData() {
        return $.cookie(cookieKey) ? JSON.parse($.cookie(cookieKey)) : {};
    }

    init();
    function init() {
        data = (typeof $.cookie(cookieKey)=='undefined') ? {} : JSON.parse($.cookie(cookieKey)); 
        console.log(data);
    }
    //计算选项结果值
    calc();
    function calc() {
        if($.isEmptyObject(data)) return;
        var housetype = data.housetype;
        var phone_num = data.phone_num;
        var phone_service = data.phone_service;
        var tv_service = data.tv_service;

        var nomal = 0,
            first = 0,
            second = 0,
            temText;

        if(housetype=='simple'){
            nomal+=calcData.nomal.internet_fee.simple ;
            first+=calcData.first.internet_fee.simple;
            second+=calcData.second.internet_fee.simple;


           //料金处理
           temText = calcData.nomal.internet_fee.simple + '円/月';

           $('.jsf-tabcontents:eq(2) .jss-monthry, .jss-monthry').text(temText);
           
           temText = '初月無料' ;
           $('.jsf-tabcontents:eq(1) .jss-monthry').text(temText);
              
           temText = calcData.second.internet_fee.simple + '円/月';
           $('.jsf-tabcontents:eq(0) .jss-monthry').text(temText);

           // //折扣处理
           // nomal+=0;
           // first+=-125;
           // second+=-125;
           // $('.jsf-tabcontents:eq(2) .jss-cam').text('なし');
           // $('.jsf-tabcontents:eq(1) .jss-cam').text('-125円');
           // $('.jsf-tabcontents:eq(0) .jss-cam').text('-125円');

        }else if(housetype=='complex'){
            nomal+=calcData.nomal.internet_fee.complex ;
            first+=calcData.first.internet_fee.complex;
            second+=calcData.second.internet_fee.complex;


           //料金处理
           temText = calcData.nomal.internet_fee.complex+'円/月';

           $('.jsf-tabcontents:eq(2) .jss-monthry,.jss-monthry').text(temText);

           temText = '初月無料' ;

           $('.jsf-tabcontents:eq(1) .jss-monthry').text(temText);

           temText = calcData.nomal.internet_fee.complex+'円/月';
           $('.jsf-tabcontents:eq(0) .jss-monthry').text(temText);

           // //折扣处理
           // nomal+=0;
           // first+=-1250;
           // second+=-1250;
           // $('.jsf-tabcontents:eq(2) .jss-cam').text('なし');
           // $('.jsf-tabcontents:eq(1) .jss-cam').text('-1250円');
           // $('.jsf-tabcontents:eq(0) .jss-cam').text('-1250円');
        }


        // if(phone_num>0&&phone_service){
        //    var price = -phone_num*2000;
        //    nomal+=price;
        //    first+=price;
        //    second+=price;
        //    $('.jsf-tabcontents:eq(2) .jss-smart').text(price+'円');
        //    $('.jsf-tabcontents:eq(1) .jss-smart').text(price+'円');
        //    $('.jsf-tabcontents:eq(0) .jss-smart').text(price+'円');
        // }else{
        //    $('.jsf-tabcontents:eq(2) .jss-smart').text('なし');
        //    $('.jsf-tabcontents:eq(1) .jss-smart').text('なし');
        //    $('.jsf-tabcontents:eq(0) .jss-smart').text('なし');
        // }

        if(phone_service){

            nomal+=calcData.nomal.tel_service.selected ;
            first+=calcData.first.tel_service.selected;
            second+=calcData.second.tel_service.selected;

            $('.jsf-tabcontents .tel-tr').show();
       
            temText = calcData.nomal.tel_service.selected + '円/月';
            $('.jsf-tabcontents:eq(2) .jss-tel,.jss-tel').text(temText);

            temText = calcData.first.tel_service.selected + '円/月';
            $('.jsf-tabcontents:eq(1) .jss-tel').text(temText);

            temText = calcData.second.tel_service.selected + '円/月';
            $('.jsf-tabcontents:eq(0) .jss-tel').text(temText);

            $('.construction').hide();

            $('.discount-tr').show();
            
            nomal+=calcData.nomal.discount;
            first+=calcData.first.discount;
            second+=calcData.second.discount;
            //设置折扣 

            temText = calcData.nomal.discount + '円/月';
            $('.jsf-tabcontents:eq(2) .jss-cam').text(temText);

            temText = calcData.first.discount + '円/月';
            
            $('.jsf-tabcontents:eq(1) .jss-cam').text(temText);
            
            temText = calcData.second.discount + '円/月';
            $('.jsf-tabcontents:eq(0) .jss-cam').text(temText);


           

        }else{
            

            nomal+=calcData.nomal.tel_service.no_selected;
            first+=calcData.first.tel_service.no_selected;
            second+=calcData.second.tel_service.no_selected;
            
            $('.jsf-tabcontents .tel-tr').hide();


            //设置折扣显示
            $('.discount-tr').hide();
            //设置建费用显示
           $('.construction').show();
                
            nomal+=calcData.nomal.construction_fee;
            first+=calcData.first.construction_fee;
            second+=calcData.second.construction_fee;

            temText = calcData.nomal.construction_fee + '円/月';
            $('.jsf-tabcontents:eq(2) .js-construnction').text(temText);

             temText = calcData.first.construction_fee + '円/月';
            $('.jsf-tabcontents:eq(1) .js-construnction').text(temText);

             temText = calcData.second.construction_fee + '円/月';
            $('.jsf-tabcontents:eq(0) .js-construnction').text(temText);
        }

        //用电视服务来代替安全服务
        if(tv_service){
            nomal+=calcData.nomal.security_fee;
            first+=calcData.first.security_fee;
            second+=calcData.second.security_fee;

            temText = calcData.nomal.security_fee + '円/月';

            $('.jsf-tabcontents:eq(2) .jss-2mon-tv,.jss-tv').text(temText);

            temText = '24ヶ月無料';
            $('.jsf-tabcontents:eq(1) .jss-tv').text(temText);

            $('.jsf-tabcontents:eq(0) .jss-tv').text(temText);

            $('.jsf-tabcontents .tv-tr').show();
        }else{
            $('.jsf-tabcontents .tv-tr').hide();
        }

        
        nomal = nomal<0 ? 0: nomal; 
        first = first<0 ? 0: first; 
        second = second<0 ? 0: second; 

        $('.jss-total').text(nomal+'円');
        $('.jss-1mon-total').text(first+'円');
        $('.jss-2mon-total').text(second+'円');

        $('.phrase2.jss-total-num').text(nomal);
        $('.usua-box .price').text(nomal+'円');


        $('.after-box .price').text(second+'円');
        $('.jss-after-total-num').text(second);
        $('.month .month_r .number').text(first);
    }

	//不同选项点击处理
	//      
	$("#jsf-housing-type-popup-handle").click(function() {
	    $("#jsf-popup-background").fadeIn(),
	    $("#jsf-popup").fadeIn(),
	    $("#jsf-housing-type-popup").fadeIn()
	});
	$("#jsf-popup-close-button, #jsf-popup-background").click(function() {
            $(".jsf-popup-inner").fadeOut(),
            $("#jsf-popup").fadeOut(),
            $("#jsf-popup-background").fadeOut()
    });
    //housetype
	$("#jsf-condition-top ul li:eq(0)").click(function() {
		$("#jsf-condition-top").hide(),
		$("#jsf-condition-first").fadeIn(),
		$("#jsf-monthly-pay").fadeIn();
        data.housetype = 'simple';
          $('input[name=plan_type]').val('ホームタイプ');
        updateCookie();
	});
	$("#jsf-condition-top ul li:eq(1)").click(function() {
		$("#jsf-condition-top").hide(),
		$("#jsf-condition-first").fadeIn();
        data.housetype = 'complex';
      
        $('input[name=plan_type]').val('マンションタイプ');
        updateCookie()
	});
    $("#jsf-condition-top ul li").click(function() {
        $("#jsf-monthly-pay").fadeIn();

    });

    //智能手机
    $("#jsf-condition-first .jsf-condition-list li:eq(0)").click(function() {
            //$("#jsf-condition-unit-count-wrapper").fadeIn(); 屏蔽之前选择
            $("#jsf-condition-sec").fadeIn();
            $("#jsf-condition-first").hide(),
            updateCookie();
            
    });
    $("#jsf-condition-first .jsf-condition-list li:eq(1)").click(function() {
            $("#jsf-condition-first").hide(),
            $("#jsf-condition-sec").fadeIn();
            
        data.phone_num = 0;
        updateCookie();
    });

  	$("#jsf-smartphone-popup-handle").click(function() {
        $("#jsf-popup-background").fadeIn();
        $("#jsf-popup").fadeIn();
        $("#jsf-smartphone-popup").fadeIn();
    });

	$(".jsf-condition-unit-count-item").click(function() {
		

		//$("#jsf-condition-unit-count-wrapper").hide(),
        data.phone_num = 1 ;
        $('input[name=phone_num]').val(data.phone_num);
        updateCookie();
	});

    $("#jsf-tel-popup-handle").click(function() {
	    $("#jsf-popup-background").fadeIn(),
	    $("#jsf-popup").fadeIn(),
	    $("#jsf-tel-popup").fadeIn()
	});

    //电话服务选择
	$("#jsf-condition-sec ul li:eq(0)").click(function() {
            $("#jsf-condition-sec").hide(),
             $("#jsf-condition-thr").fadeIn(); //去掉电视修改 改成安全
            //$("#jsf-popup-background").fadeIn(),
            data.phone_service = 1;
            updateCookie();
            $('input[name=tel_service]').val('利用する');
    });
    $("#jsf-condition-sec ul li:eq(1)").click(function() {
            $("#jsf-condition-sec").hide(),
            $("#jsf-condition-thr").fadeIn();
            //$("#jsf-condition-four").fadeIn();
            data.phone_service = 0;
            updateCookie();
            $('input[name=tel_service]').val('利用しない');
    });


    //电视mask 
    $("#jsf-tv-popup-handle").click(function() {
            $("#jsf-popup-background").fadeIn(),
            $("#jsf-popup").fadeIn(),
            $("#jsf-tv-popup").fadeIn();
     });

    //电视点击选择  改成安全的选择
    $("#jsf-condition-thr ul li:eq(0)").click(function() {
            $("#jsf-condition-thr").hide();
            //$("#jsf-condition-four").fadeIn(); 隐藏邮件选择发送
             data.tv_service = 1;
            updateCookie();
             $("#jsf-result").fadeIn(800);//直接显示结果
            $('input[name=tv_service]').val('利用する');
    });
    $("#jsf-condition-thr ul li:eq(1)").click(function() {
            $("#jsf-condition-thr").hide();
           // $("#jsf-condition-four").fadeIn();
            data.tv_service = 0;
            updateCookie();
             $("#jsf-result").fadeIn(800);//直接显示结果
             $('input[name=tv_service]').val('利用しない');
    }),
    

    //重新开始
    $(".jsf-resimulation").click(function() {
        l = !1,
        $("#jsf-result").hide(),
        $("#jsf-condition-selection").show(),
        $("#jsf-condition-top").fadeIn()
    })
    //返回按钮
    $("#jsf-beforebtn1").click(function() {
	    $("#jsf-condition-first").hide();
	    $("#jsf-condition-top").fadeIn();
	});
    $("#jsf-beforebtn2").click(function() {
            $("#jsf-condition-sec").hide();
            $("#jsf-condition-first").fadeIn();
    })
    $("#jsf-beforebtn3").click(function() {
        $("#jsf-condition-thr").hide();
        $("#jsf-condition-sec").fadeIn();
    })
    $("#jsf-beforebtn4").click(function() {
        $("#jsf-condition-four").hide();
        $("#jsf-condition-thr").fadeIn();

    });
    // $("#jsf-condition-four ul li:eq(0)").click(function() {
    //         $("#jsf-mail-box").fadeIn(800);
    // })
    // $("#jsf-condition-four ul li:eq(1)").click(function() {
    //     $("#jsf-condition-selection").hide();
    //     $("#jsf-condition-four").hide();
    //     $("#jsf-result").fadeIn(2e3);
    // });
    //关闭结果按钮
    $("#jsf-close-button").click(function() {
        $("#jsf-notice").slideUp(function() {
            $("#jsf-close-button").hide(),
            $("#jsf-detail-view-button").show()
        })
    });

    $(".js-btn-close").click(function() {
        return $(".js-modal-mailform").removeClass("modal-on").fadeOut("100");
    });
    //打开按钮
    $("#jsf-detail-view-button").click(function() {
        $("#jsf-notice").slideDown(function() {
                $("#jsf-detail-view-button").hide(),
                $("#jsf-close-button").show()
        })
   }),




    //发送邮件打开窗口
    $(".modal-trg a, #close-trg, #close-trg2, .modal-style .bg-modal, .modal-style2 .bg-modal").click(function() {
        var o = $(this).attr("href");
        return $(o).hasClass("modal-on") ? $(o).removeClass("modal-on").fadeOut("100") : $(o).addClass("modal-on").fadeIn("100"),
        !1
    });

    $("#send_mail .send_mail-close, #send_mail .bg-modal").click(function() {
        return $("#send_mail").fadeOut(),
        !1
    })

    $(".jsf-tabmenu-item").click(function() {
            var s = $(this);
            if (s.hasClass("active"))
                return !1;
            s.closest(".jsf-tabmenu").find(".jsf-tabmenu-item").removeClass("active"),
            s.addClass("active");
            var t = s.index()
              , a = s.closest(".jsf-tab").find(".jsf-tabcontents");
            a.hide(),
            a.eq(t).stop().fadeIn("slow")
    })


    $('.mail-box-resultButton').click(function(){
        var email = $('input[name=i_mail_simresult]').val();
        if(email==''){
            $('input[name=i_mail_simresult]').focus();
        }
    })

    $("#form1").validate({
        rules:{
            i_name_t:"required",
            i_tel_t:"required",
            i_tel_t:"required",
            i_zip:"required",
            i_tel:{
                required:true,
                minlength:10,
                maxlength:11,
            },

        },
        // errorPlacement: function(error, element) { 

        //     element.parents('.input-box').find('.pop-require').show();
        //     element.parents('.input-box').find('.pop-require').append(error);
        //     // error.appendTo(element.next(''));  
        // },
        // success: function(label) {
        //     console.log(label);
        //     label.parents('.input-box').find('.pop-require').hide();            
        //     //label.addClass("valid").text("Ok!")
        // }
     });

    $('input[name=check_mode]').click(function(event) {
       var email = $('input[name=i_mail_simresult_m]').val();
       var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
            if(reg.test(email)){
                var html = $('#jsf-notice').prop('outerHTML');
                $.ajax({
                    url: 'email.php',
                    type: 'POST',
                    data: {email:email,html:html,result:data},
                })
                .done(function(res) {
                    if(res){
                        $('#send_mail').show();
                        $('.btn-close').click();
                   }else{
                        alert('Failed to send the email. Please contact the administrator');
                   }
                })
                .fail(function() {
                    console.log("error");
                })
                .always(function() {
                    console.log("complete");
                });
                
            }else{
                //不是邮箱格式焦点定位。
                 $('input[name=i_mail_simresult_m]').focus();
            }
    });

    $('.mail-box-resultButton').click(function(event) {
         var email = $('input[name=i_mail_simresult]').val();
        var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
            if(reg.test(email)){
                var html = $('#jsf-notice').prop('outerHTML');
                $.ajax({
                    url: 'email.php',
                    type: 'POST',
                    data: {email:email,html:html,result:data},
                })
                .done(function(res) {
                   if(res){
                        $('#send_mail').show();
                   }else{
                        alert('Failed to send the email. Please contact the administrator');
                   }
                })
                .fail(function() {
                    console.log("error");
                })
                .always(function() {
                    console.log("complete");
                });
                
            }else{
                //不是邮箱格式焦点定位。
                 $('input[name=i_mail_simresult]').focus();
            }
    });

})
